package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.CategoryDao;
import com.itheima.SetmealDao;
import com.itheima.SetmealDishDao;
import com.itheima.SetmealService;
import com.itheima.bean.Category;
import com.itheima.bean.Params;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishDao setmealDishDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<Setmeal> findUserSetmeal(Long categoryId, int status) {
        LambdaQueryWrapper<Setmeal> sqw=new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId,categoryId);
        sqw.eq(Setmeal::getStatus,status);
        return setmealDao.selectList(sqw);

    }

    @Override
    public int updateStatus(int status, List<Long> list) {
              LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
              qw.in(Setmeal::getId,list);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
      int row = setmealDao.update(setmeal,qw);

        return row;
    }

    @Override
    public int delete(List<Long> list) {

        for (Long id : list) {
            Setmeal setmeal = setmealDao.selectById(id);
            if (setmeal.getStatus()==1){
                throw new CustomException("当前套餐处于起售状态，禁止删除！");
            }
        }
        int row1 = setmealDao.deleteBatchIds(list);
        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.in(SetmealDish::getSetmealId,list);
        int row2 = setmealDishDao.delete(qw);

        return row1>0&&row2>0?1:0;
    }

    @Override
    public int add(SetmealDto setmealDto) {
        int row = setmealDao.insert(setmealDto);
        int count=0;
        if (row>0){
          List<SetmealDish> list = setmealDto.getSetmealDishes();
            Long setmealId = setmealDto.getId();
            if (list!=null&&list.size()>0){
              for (SetmealDish setmealDish : list) {
                   setmealDish.setSetmealId(setmealId);
                  count+= setmealDishDao.insert(setmealDish);
              }
          }
        }
        return (row>0&&count==setmealDto.getSetmealDishes().size())?1:0;
    }

    @Override
    public IPage<SetmealDto> findPage(Params params) {
        IPage<Setmeal> p = new Page<>(params.getPage(), params.getPageSize());
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.like(params.getName() != null, Setmeal::getName, params.getName());
        IPage<Setmeal> setmealIPage = setmealDao.selectPage(p, sqw);

        List<SetmealDto> list= new ArrayList<>();
        IPage<SetmealDto> setmealDtoPage = new Page<>();
        for (Setmeal setmeal : setmealIPage.getRecords()) {
            Category category = categoryDao.selectById(setmeal.getCategoryId());
            String name = category.getName();
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(name);
            BeanUtils.copyProperties(setmeal,setmealDto);
            list.add(setmealDto);
        }
        setmealDtoPage.setRecords(list);

        return setmealDtoPage;

    }
}
